package com.applepaihs.controller;

import cn.hutool.core.bean.BeanUtil;
import com.applepaihs.domain.dto.PageDto;
import com.applepaihs.domain.dto.RedropesettleFormDto;
import com.applepaihs.domain.query.RedropesettleQuery;
import com.applepaihs.domain.vo.RedropesettleVo;
import com.applepaihs.model.Redropesettle;
import com.applepaihs.service.RedropesettleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("结算单管理接口")
@RequestMapping("/redropesettles")
@RestController
@RequiredArgsConstructor
public class RedropesettleController {

    private final RedropesettleService redropesettleService;

    @ApiOperation("新增结算单记录")
    @PostMapping
    public void saveRedropesettle(@RequestBody RedropesettleFormDto redropesettleFormDto){
        Redropesettle redropesettle = BeanUtil.copyProperties(redropesettleFormDto,Redropesettle.class);
        redropesettleService.save(redropesettle);
    }

    @ApiOperation("删除结算单")
    @DeleteMapping("{id}")
    public void deleteRedropesettleById(@ApiParam("结算单id") @PathVariable("id") Long id){
        redropesettleService.removeById(id);
    }


    @ApiOperation("根据id查询结算单")
    @GetMapping("{id}")
    public RedropesettleVo getRedropesettleById(@ApiParam("结算单id") @PathVariable("id") Long id){
        Redropesettle redropesettle = redropesettleService.getById(id);
        return BeanUtil.copyProperties(redropesettle, RedropesettleVo.class);
    }

    @ApiOperation("根据id批量查询结算单")
    @GetMapping
    public List<RedropesettleVo> getRedropesettleById(@ApiParam("结算单id集合") @RequestParam("ids") List<Long> ids){
        List<Redropesettle> redropesettles = redropesettleService.listByIds(ids);
        return BeanUtil.copyToList(redropesettles, RedropesettleVo.class);
    }

    @ApiOperation("扣减结算单金额接口")
    @GetMapping("/{id}/deduction/{money}")
    public void deductionPrice(@ApiParam("结算单id") @PathVariable("id") Long id,
                                     @ApiParam("扣减的金额") @PathVariable("money") Integer money){
        redropesettleService.deductionPrice(id,money);
    }

    @ApiOperation("根据复杂条件查询结算单")
    @GetMapping("/list")
    public List<RedropesettleVo> queryRedropesettles(RedropesettleQuery query){
        List<Redropesettle> redropesettles = redropesettleService.queryRedropesettles(query.getSettleMonth(),query.getRoleType(),query.getActorWeixinuserId(),query.getFlowNum());
        return BeanUtil.copyToList(redropesettles, RedropesettleVo.class);
    }

    @ApiOperation("根据复杂条件分页查询结算单")
    @GetMapping("/page")
    public PageDto<RedropesettleVo> queryRedropesettlesPage(RedropesettleQuery query){
        return redropesettleService.queryRedropesettlesPage(query);
    }

}
